var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.get('/', function(req, res, next) {
    console.log(req.url)
    let zcbh = _.trim(String(req.query.zcbh));
    let tmbh = _.trim(String(req.query.tmbh));

    let data = {
        isSuccess: false,
        data: {},
        msg: '资产数据获取失败！'
    };
    let whereClause = '';
    console.log('资产编号：' + zcbh);
    console.log('条码编号：' + tmbh);
    if(zcbh != '' && zcbh != undefined) {
        console.log("1")
        whereClause = "zcbh='" + zcbh + "'"; 
     } else if(tmbh != '' && tmbh != undefined){
        console.log("2")
        whereClause = "tmbh='" + tmbh + "'"; 
     } else {
        data.msg = "参数传递错误！";
        console.log("3")
        return res.json(data);
     }
     /**
      * 资产信息
      */
    let sql = 'select * from equipmentinfo where ' + whereClause;
    /**
     * 故障记录
     */
    let sql1 = "SELECT s2.id AS id, s2.equipmentid, s2.recorddate, s2.isneedhelp, s2.assistant, s3.name, s2.type, s2.desc, s2.isfinished,s2.finishdate,s2.depart, s2.workshop" 
    + " from equipmentinfo as s1 INNER JOIN event_record AS s2 ON s1.id=s2.equipmentid AND (s1."+ whereClause + ") INNER JOIN sys_user AS s3 ON s2.recorderid=s3.id ORDER BY id DESC;"
    console.log(sql);
    /**
     * 变更记录
     */
    let sql2 = "SELECT s2.id AS id, s2.equipmentid, s2.depart, s2.workshop, s2.user, s2.contact, s2.desc, s2.location, s2.recorddate, s3.name, s2.state, s2.scrapreason" 
    + " from equipmentinfo as s1 INNER JOIN equipment_change_record AS s2 ON s1.id=s2.equipmentid AND (s1."+ whereClause + ")INNER JOIN sys_user AS s3 ON s2.recorderid=s3.id ORDER BY id DESC;"
    
    /**
     * 维修记录
     */
    let sql3 = "SELECT s2.id AS id, s2.equipmentid, s2.state, s2.surveyor, s2.thecontact, s2.contact, s2.desc, s2.recorddate, s3.name, s2.isfinished, s2.finisheddate " 
    + "from equipmentinfo as s1 INNER JOIN equipment_maintenance AS s2 ON s1.id=s2.equipmentid AND (s1."+ whereClause + ")INNER JOIN sys_user AS s3 ON s2.recorderid=s3.id ORDER BY id DESC;"
    
    console.log("sql： " + sql)
    console.log("sql2： " + sql2)
    console.log("sql3： " + sql3)
    db.execute(sql, function(err, rows) {
        if(err) {
            console.log('1');
            return res.json(data);
        } else {
            if(rows.length == 1 ) {
                data.data['baseInfo'] = rows;
                console.log("sql1:" + sql1);
               db.execute(sql1, function(err1, rows1) {
                    if(err1) {
                        console.log('2');
                        return res.json(data);
                    } else {
                        data.data['hitch'] = rows1;
                        console.log(sql2);
                        db.execute(sql2, function(err2, rows2) {
                            if(err2) {
                                console.log('3');
                                return res.json(data);
                            } else {
                                data.data['change'] = rows2;
                                console.log("sql3:" + sql3);
                                db.execute(sql3, function(err3, rows3) {
                                    if(err3) {
                                        console.log('4');
                                        return res.json(data)
                                    } else {
                                        data.data['maintain'] = rows3;
                                        data.isSuccess = true;
                                        data.msg = "资产数据获取成功！";
                                        console.log('5');
                                        return res.json(data);
                                    }
                                })
                            }
                        })
                    }
               })
            } else if(rows.length < 0){
                data.msg = "资产数据获取失败！";
                console.log('6');
                return res.json(data);
            } else {
                data.msg = "资产数据出现错误，请联系管理员！";
                
                console.log(data);
                console.log('7');
                return res.json(data);
            }
        }
    })
});

module.exports = router;